package com.ruoyi.maode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.maode.domain.dto.AddPartDTO;
import com.ruoyi.maode.domain.dto.QueryPartDTO;
import com.ruoyi.maode.domain.entity.Part;
import com.ruoyi.maode.domain.entity.PartBatch;
import com.ruoyi.maode.domain.vo.PartSizeVO;
import com.ruoyi.maode.service.PartService;
import com.ruoyi.maode.service.PartSizeService;
import com.ruoyi.web.controller.common.CommonController;
import com.ruoyi.web.core.config.ResultDTO;
import com.ruoyi.web.util.PageQuery;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 零部件Controller
 * @author zhangwenqiang
 */

@RestController
@RequestMapping("/part")
public class PartController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(PartController.class);

    @Autowired
    private PartService partService;

    @ApiOperation(value = "新增零件")
    @PostMapping("/add")
    public ResultDTO addPart(@RequestBody AddPartDTO dto) {
        int result = partService.addPart(dto);
        return result > 0 ? ResultDTO.success("新增成功！") : ResultDTO.error("新增失败！");
    }

    @ApiOperation(value = "删除零件")
    @PutMapping("/deleteById")
    public ResultDTO deleteById(@RequestBody Long id) {
        int result = partService.deleteById(id);
        return result > 0 ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation(value = "批量删除零件")
    @PutMapping("/deleteByIds")
    public ResultDTO deleteByIds(@RequestBody List<Long> ids) {
        int result = partService.deleteByIds(ids);
        return result > 0 ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation(value = "编辑零件")
    @PutMapping("/edit")
    public ResultDTO edit(@RequestBody AddPartDTO dto) {
        int result = partService.editPart(dto);
        return result > 0 ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation(value = "查询零件列表")
    @PostMapping("/list")
    public ResultDTO<Page<Part>> getPage(@RequestBody PageQuery<QueryPartDTO> dto) {
        Page<Part> partList = partService.selectPartList(dto);
        return ResultDTO.success(partList);
    }

    @ApiOperation(value = "获取零件详情")
    @GetMapping("/getDetail/{id}")
    public ResultDTO getDetail(@PathVariable("id") Long id) {
        return ResultDTO.success(partService.getDetail(id));
    }

    @ApiOperation(value = "根据物料编码，模糊查询零件数据")
    @GetMapping("/getByPartCode/{code}")
    public ResultDTO getByPartCode(@PathVariable("code") String code) {
        return ResultDTO.success(partService.list(new LambdaQueryWrapper<Part>()
                .eq(Part::getIsDelete, Constants.CONSTANTS_ZERO)
                .like(Part::getPartCode,code)));
    }

    @ApiOperation(value = "根据物料名称，模糊查询零件数据")
    @GetMapping("/getByPartName/{partName}")
    public ResultDTO getByPartName(@PathVariable("partName") String partName) {
        return ResultDTO.success(partService.list(new LambdaQueryWrapper<Part>().like(Part::getPartName,partName)));
    }
}
